package com.example.smart.base.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.CallSuper
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.utils.am.ActManger
import com.example.smart.R
import com.example.smart.base.dialog.LoadingDialog
import com.example.smart.base.utils.getViewBinding
import com.example.smart.databinding.IncludeTabBinding
import com.example.smart.utils.DisplayUtils

abstract class BaseActivity<V : ViewBinding> : AppCompatActivity() {
    protected lateinit var binding: V
    private lateinit var contentViewGroup: ViewGroup

    protected lateinit var includeBinding: IncludeTabBinding

    private val loadingDialog by lazy {
        LoadingDialog(this@BaseActivity)
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        beforeOnCreate(savedInstanceState)
        super.onCreate(savedInstanceState)
        binding = getViewBinding(LayoutInflater.from(this@BaseActivity).inflate(layoutID(), null))
        setAppView(binding.root)
        afterOnGetBinding(savedInstanceState)
        ActManger.addActivity(this)

        initView()
        initListener()
        initData()
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        initData()
    }

    open fun beforeOnCreate(savedInstanceState: Bundle?) {
    }

    @CallSuper
    open fun afterOnGetBinding(savedInstanceState: Bundle?) {
        if (isNeedAllTab()) {
            val view = binding.root.findViewById<View>(R.id.Include)
            includeBinding = IncludeTabBinding.bind(view)

            includeBinding.IncludeHome.setOnDoubleClickListener {
                DisplayUtils.toMain(this@BaseActivity, 0, isNeedFinish = false)
            }

            includeBinding.IncludeStudy.setOnDoubleClickListener {
                DisplayUtils.toMain(this@BaseActivity, 1, isNeedFinish = false)
            }

            includeBinding.IncludeCommunity.setOnDoubleClickListener {
                DisplayUtils.toMain(this@BaseActivity, 2, isNeedFinish = false)
            }

            includeBinding.IncludeMine.setOnDoubleClickListener {
                DisplayUtils.toMain(this@BaseActivity, 3, isNeedFinish = false)
            }
        }

    }

    /**
     * 视图创建之前改变视图使用
     *
     * @param view
     * @return 修改后的视图
     */
    open fun beforeCreateView(view: View) = view

    protected abstract fun layoutID(): Int

    protected abstract fun initView()

    protected open fun initListener() {}

    protected open fun initData() {}

    /**
     * 设置app视图，保证展示空白视图时能有头部
     */
    private fun setAppView(contentView: View) {
        val view = beforeCreateView(contentView)
        if (this::contentViewGroup.isInitialized) {
            contentViewGroup.removeAllViews()
            contentViewGroup.addView(view)
        } else {
            setContentView(view)
            contentViewGroup = findViewById(android.R.id.content)
        }
    }

    open fun showLoading() {
        loadingDialog.show()
    }

    open fun showLoading(msg: String) {
        loadingDialog.text = msg
        loadingDialog.show()
    }

    open fun hideLoading() {
        loadingDialog.dismiss()
    }

    override fun onDestroy() {
        ActManger.removeActivity(this)
        super.onDestroy()
    }

    open fun isNeedAllTab() = false
}